capture log close

//  Date:         	08/07/2018
//  task:        	output cleanup
//  project:     	World Development
//  user-written: 	sutex, sxpose, center  

//  #0
//  setup

clear all
macro drop _all
set more off
set matsize 800 

set scheme plottig, permanently // plottig is not installed in Stata by default

do make_index_gr

log using 2.analysis_no_imputation_health.log, replace

*********************************************************
// #1: load data
*********************************************************

use ubridge_health.dta

*********************************************************
// #2: drop variables that have all missing values
*********************************************************
// findname is not installed in Stata by default
findname, all(missing(@))
drop `r(varlist)'
drop V_inv_employees_absent_unauth_mB V_employees_absent_unauth_mB V_percent_empl_ab_unauth_mB V_q50_dho_fire_REC_mE 

*************************************************************
// #3: plot raw outcomes overtime
*************************************************************
local monitoring V_dho_visit V_dho_calls V_freq_calls V_BE_q67_q62 A_insp_qtr A_insp_yr
local effort V_pctpresent V_unauthorized_absence RegisterBook V_outreach
local input V_inv_so_antimalarials V_inv_so_ors_analysis A_so_antimalarial A_so_ors A_total_so_rate A_Funds_Received 
local utilization A_OutPat_Att_Total A_OutPat_Ref_Total A_total_attendence A_OutPat_Diag_Total A_TI_Total A_CI_Tot A_ANC_IPT A_ANC_Iron A_ANC_ITN A_ANC_Test_Syph A_Mat_Admit A_Mat_Deliver_InUnit A_Mat_Moth_VitA A_PN_Att A_CH_VitA_Tot A_CH_Deworm_Tot 


foreach v in `monitoring' `effort' `input' `utilization'  {
        local l`v' : variable label `v'
            if `"`l`v''"' == "" {
            local l`v' "`v'"
        }
}

*********************************************************
// #4: produce summary statistics for outcome variables 
*********************************************************

*********************************************************
* Table 2 in Supplementary Information
*********************************************************

sum V_dho_visit V_dho_calls V_freq_calls V_BE_q67_q62 A_insp_qtr A_insp_yr V_pctpresent V_unauthorized_absence RegisterBook V_outreach V_inv_so_antimalarials V_inv_so_ors_analysis A_so_antimalarial A_so_ors A_total_so_rate A_Funds_Received  A_OutPat_Att_Total A_OutPat_Ref_Total A_total_attendence A_OutPat_Diag_Total A_TI_Total A_CI_Tot A_ANC_IPT A_ANC_Iron A_ANC_ITN A_ANC_Test_Syph A_Mat_Admit A_Mat_Deliver_InUnit A_Mat_Moth_VitA A_PN_Att A_CH_VitA_Tot A_CH_Deworm_Tot 

*********************************************************
// #5: reshape wide
*********************************************************
reshape wide V_open-V_pctpresent, i(cluster_id) j(V_audit_period) 

foreach v in `monitoring' `effort' `input' `utilization' {
        label var `v'1 "`l`v'' (baseline)" 
		label var `v'2 "`l`v'' (midline)"
		label var `v'3 "`l`v'' (endine)"
}

foreach var in DataSetID DISTRICT district treat quasi_control{
	move `var' V_open1
	}

*********************************************************	
//  #6: merge census data
*********************************************************
merge 1:1 cluster_id using ubridge_clusters.dta
drop _m

gen freq_high_calls1= V_freq_calls1
gen freq_high_calls2= V_freq_calls2
gen freq_high_calls3= V_freq_calls3

*********************************************************
// #7: Table for balance of outcomes variable in baseline 
*********************************************************

local monitoring V_dho_visit V_dho_calls V_freq_calls V_BE_q67_q62 A_insp_qtr A_insp_yr
local effort RegisterBook V_outreach
local input V_inv_so_antimalarials V_inv_so_ors_analysis A_so_antimalarial A_so_ors A_total_so_rate A_Funds_Received 
local utilization A_OutPat_Att_Total A_OutPat_Ref_Total A_total_attendence A_OutPat_Diag_Total A_TI_Total A_CI_Tot A_ANC_IPT A_ANC_Iron A_ANC_ITN A_ANC_Test_Syph A_Mat_Admit A_Mat_Deliver_InUnit A_Mat_Moth_VitA A_PN_Att A_CH_VitA_Tot A_CH_Deworm_Tot 

preserve
tempfile tmpfile

foreach y in `monitoring' `effort' `input' `utilization' { 
	reg `y'1 treat, r
	local lab : variable label `y'1
	regsave using  "`tmpfile'" /// // regsave is not installed in Stata by default
		, addlabel(Variable,"`lab'") pval table(`y', parentheses(stderr) format(%8.2fc)) `replace'
	local replace "append"
	}

use "`tmpfile'", clear
replace var = subinstr(var,"_coef","",1)
replace var = "" if strpos(var,"stderr")!=0

*transpose the data 
	sxpose, clear // sxpose is not installed in Stata by default

*dropping constant sd 
drop _var5 _var6 _var8 _var2
order _var9 _var4 _var1  _var3 _var7

unab vars : _var9 - _var7
foreach v in `vars' {
	replace `v' = substr(`v' , 2 , .) if regexm(`v' , "^_")
	local n = `v'[1]
	rename `v' `n'
	}
drop in 1

lab var cons "C mean"
lab var treat "T-C"
lab var treat_pval "P-val"
lab var N "N"

*********************************************************
* Table 6 in Supplementary Information
*********************************************************

list, sep(5)

restore

*********************************************************
// #8: define covariates
*********************************************************

	replace HC_dist=HC_dist/1000
	lab var HC_dist "Distance to health center (km)" 
	
	replace Arua_dist=Arua_dist/1000
	lab var Arua_dist "Distance to Arua (km)" 
	
	gen ldist=log(Arua_dist)
	lab var ldist "Log distance to Arua" 
	su Arua_dist ldist, de
	lab var fractionalization "Religious fractionalization"

gen lpop=log(adult_pop)
lab var lpop "Log adult population"
su adult_pop lpop, de

foreach y in lpop age poverty_census lugbara_share HHI_Ethnicity polarization HHI_Religion literacy education employed_share Nonagriculture_share Arua_dist{
	 egen `y'_median=median(`y')
	 gen `y'_miss = `y'==.
	 tab `y'_miss
	 replace `y' = `y'_median if `y'==.
	 center `y'  , standardize // center is not installed in Stata by default
	 drop `y'_median
}	

*********************************************************
// #9: Construct Health indices
*********************************************************

*******************
* health monitoring 
*******************

forvalues i=1(1)3{
	gl monitoring`i' V_dho_visit`i' V_dho_calls`i' V_freq_calls`i' V_BE_q67_q62`i' A_insp_qtr`i' A_insp_yr`i'
}

foreach var in $monitoring1 $monitoring2 $monitoring3{
	quietly summarize `var' if treat==0
	local `var'_mean= r(mean)
	local `var'_sd= r(sd) 
	gen c_`var' = (`var'-``var'_mean')/``var'_sd'
	qui egen mean_std_`var'=mean(c_`var') if treat==1
	replace c_`var' = mean_std_`var' if treat==1 & c_`var'==.
	replace c_`var' = 0 if treat==0 & c_`var'==.
	corr `var' c_`var'
	}
	
forvalues i=1(1)3{	
	alpha c_V_dho_visit`i'- c_A_insp_yr`i', std item
	egen monitoring_index`i'=rowmean(c_V_dho_visit`i' c_V_dho_calls`i' c_V_freq_calls`i' c_V_BE_q67_q62`i' c_A_insp_qtr`i' c_A_insp_yr`i')									
	lab var monitoring_index`i' "Health monitoring index"
	}

	g wgt = 1
	gen stdgroup=treat==0
	
	make_index_gr health_monitoring1 wgt stdgroup c_V_dho_visit1 c_V_dho_calls1 c_V_freq_calls1 c_V_BE_q67_q621 c_A_insp_qtr1 c_A_insp_yr1
	make_index_gr health_monitoring2 wgt stdgroup c_V_dho_visit2 c_V_dho_calls2 c_V_freq_calls2 c_V_BE_q67_q622 c_A_insp_qtr2 c_A_insp_yr2
	make_index_gr health_monitoring3 wgt stdgroup c_V_dho_visit3 c_V_dho_calls3 c_V_freq_calls3 c_V_BE_q67_q623 c_A_insp_qtr3 c_A_insp_yr3
	
	corr index_health_monitoring1 monitoring_index1
	su index_health_monitoring1 monitoring_index1
	su index_health_monitoring1 monitoring_index1 if treat==0
	
*******************
* Health effort
*******************

forvalues i=1(1)3{
	gl effort`i' V_pctpresent`i' V_unauthorized_absence`i' RegisterBook`i' V_outreach`i'
	}

su $effort1 $effort2 $effort3
	
foreach var in $effort1 $effort2 $effort3{
	quietly summarize `var' if treat==0
	local `var'_mean= r(mean)
	local `var'_sd= r(sd) 
	gen c_`var' = (`var'-``var'_mean')/``var'_sd'
	qui egen mean_std_`var'=mean(c_`var') if treat==1
	replace c_`var' = mean_std_`var' if treat==1 & c_`var'==.
	replace c_`var' = 0 if treat==0 & c_`var'==.
	}
	
forvalues i=1(1)3{	
	alpha c_V_pctpresent`i' c_V_unauthorized_absence`i' c_RegisterBook`i' c_V_outreach`i', std item
	}

	egen effort_index1=rowmean(c_RegisterBook1 c_V_outreach1)									
	egen effort_index2=rowmean(c_V_pctpresent2 c_V_unauthorized_absence2 c_RegisterBook2 c_V_outreach2)									
	egen effort_index3=rowmean(c_V_pctpresent3 c_V_unauthorized_absence3 c_RegisterBook3 c_V_outreach3)									
	
	make_index_gr health_effort1 wgt stdgroup c_RegisterBook1 c_V_outreach1
	make_index_gr health_effort2 wgt stdgroup c_V_pctpresent2 c_V_unauthorized_absence2 c_RegisterBook2 c_V_outreach2
	make_index_gr health_effort3 wgt stdgroup c_V_pctpresent3 c_V_unauthorized_absence3 c_RegisterBook3 c_V_outreach3

forvalues i=1(1)3{
	corr index_health_effort`i' effort_index`i'
	su index_health_effort`i' effort_index`i'
	su index_health_effort`i' effort_index`i' if treat==0
	su index_health_effort`i' effort_index`i' if treat==1
}

**************** 
* health inputs
****************

forvalues i=1(1)3{
	gl inputs`i' V_inv_so_antimalarials`i' V_inv_so_ors_analysis`i' A_so_antimalarial`i' A_so_ors`i' A_total_so_rate`i' A_Funds_Received`i'	
}

su $inputs1 $inputs2 $inputs3

foreach var in $inputs1 $inputs2 $inputs3{
	quietly summarize `var' if treat==0
	local `var'_mean= r(mean)
	local `var'_sd= r(sd) 
	gen c_`var' = (`var'-``var'_mean')/``var'_sd'
	qui egen mean_std_`var'=mean(c_`var') if treat==1
	replace c_`var' = mean_std_`var' if treat==1 & c_`var'==.
	replace c_`var' = 0 if treat==0 & c_`var'==.
	}

forvalues i=1(1)3{	
	alpha c_V_inv_so_antimalarials`i' - c_A_Funds_Received`i', std item 
	}

	egen input_index1=rowmean(c_V_inv_so_antimalarials1 c_V_inv_so_ors_analysis1 c_A_so_antimalarial1 c_A_so_ors1 c_A_total_so_rate1 c_A_Funds_Received1)									
	egen input_index2=rowmean(c_V_inv_so_antimalarials2 c_V_inv_so_ors_analysis2 c_A_so_antimalarial2 c_A_so_ors2 c_A_Funds_Received2)
	egen input_index3=rowmean(c_V_inv_so_antimalarials3 c_V_inv_so_ors_analysis3 c_A_so_antimalarial3 c_A_so_ors3 c_A_total_so_rate3 c_A_Funds_Received3)
	
	make_index_gr health_input1 wgt stdgroup c_V_inv_so_antimalarials1 c_V_inv_so_ors_analysis1 c_A_so_antimalarial1 c_A_so_ors1 c_A_total_so_rate1 c_A_Funds_Received1
	make_index_gr health_input2 wgt stdgroup c_V_inv_so_antimalarials2 c_V_inv_so_ors_analysis2 c_A_Funds_Received2
	make_index_gr health_input3 wgt stdgroup c_V_inv_so_antimalarials3 c_V_inv_so_ors_analysis3 c_A_so_antimalarial3 c_A_so_ors3 c_A_total_so_rate3 c_A_Funds_Received3

forvalues i=1(1)3{	
	corr index_health_input`i' input_index`i'
	su index_health_input`i' input_index`i'
	su index_health_input`i' input_index`i' if treat==0
	su index_health_input`i' input_index`i' if treat==1
}

******************** 
* health utilization
******************** 
forvalues i=1(1)3{
	gl utilization`i' A_OutPat_Att_Total`i' A_total_attendence`i' A_OutPat_Ref_Total`i' A_OutPat_Diag_Total`i' A_TI_Total`i' A_CI_Tot`i' ///
	A_ANC_IPT`i' A_ANC_Iron`i' A_ANC_ITN`i' A_ANC_Test_Syph`i' A_Mat_Admit`i' A_Mat_Deliver_InUnit`i' A_Mat_Moth_VitA`i' A_PN_Att`i' ///
	A_CH_VitA_Tot`i' A_CH_Deworm_Tot`i' 	
	}

foreach var in $utilization1 $utilization2 $utilization3{
	quietly summarize `var' if treat==0
	local `var'_mean= r(mean)
	local `var'_sd= r(sd) 
	gen c_`var' = (`var'-``var'_mean')/``var'_sd'
	qui egen mean_std_`var'=mean(c_`var') if treat==1
	replace c_`var' = mean_std_`var' if treat==1 & c_`var'==.
	replace c_`var' = 0 if treat==0 & c_`var'==.
	}

forvalues i=1(1)3{
	alpha c_A_OutPat_Att_Total`i' c_A_total_attendence`i' c_A_OutPat_Ref_Total`i' c_A_OutPat_Diag_Total`i' c_A_TI_Total`i'  c_A_CI_Tot`i' c_A_ANC_IPT`i' c_A_ANC_Iron`i' c_A_ANC_ITN`i' c_A_ANC_Test_Syph`i' c_A_Mat_Admit`i' c_A_Mat_Deliver_InUnit`i' c_A_Mat_Moth_VitA`i' c_A_PN_Att`i' c_A_CH_VitA_Tot`i' c_A_CH_Deworm_Tot`i', std item 									
	}

	egen utilization_index1=rowmean(c_A_OutPat_Att_Total1 c_A_total_attendence1 c_A_OutPat_Ref_Total1 c_A_OutPat_Diag_Total1 c_A_TI_Total1  c_A_CI_Tot1 c_A_ANC_IPT1 c_A_ANC_Iron1 c_A_ANC_ITN1 c_A_ANC_Test_Syph1 c_A_Mat_Admit1 c_A_Mat_Deliver_InUnit1 c_A_Mat_Moth_VitA1 c_A_PN_Att1 c_A_CH_VitA_Tot1 c_A_CH_Deworm_Tot1)
	egen utilization_index2=rowmean(c_A_OutPat_Att_Total2 c_A_total_attendence2 c_A_OutPat_Ref_Total2 c_A_OutPat_Diag_Total2 c_A_TI_Total2  c_A_CI_Tot2 c_A_ANC_IPT2 c_A_ANC_Iron2 c_A_ANC_ITN2 c_A_ANC_Test_Syph2 c_A_Mat_Admit2 c_A_Mat_Deliver_InUnit2 c_A_Mat_Moth_VitA2 c_A_PN_Att2 c_A_CH_VitA_Tot2 c_A_CH_Deworm_Tot2)
	egen utilization_index3=rowmean(c_A_OutPat_Att_Total3 c_A_total_attendence3 c_A_OutPat_Ref_Total3 c_A_OutPat_Diag_Total3 c_A_TI_Total3  c_A_CI_Tot3 c_A_ANC_IPT3 c_A_ANC_Iron3 c_A_ANC_ITN3 c_A_ANC_Test_Syph3 c_A_Mat_Admit3 c_A_Mat_Deliver_InUnit3 c_A_Mat_Moth_VitA3 c_A_PN_Att3 c_A_CH_VitA_Tot3 c_A_CH_Deworm_Tot3)
	
	make_index_gr health_utilization1 wgt stdgroup c_A_OutPat_Att_Total1 c_A_total_attendence1 c_A_OutPat_Ref_Total1 c_A_OutPat_Diag_Total1 c_A_TI_Total1  c_A_CI_Tot1 c_A_ANC_IPT1 c_A_ANC_Iron1 c_A_ANC_ITN1 c_A_ANC_Test_Syph1 c_A_Mat_Admit1 c_A_Mat_Deliver_InUnit1 c_A_Mat_Moth_VitA1 c_A_PN_Att1 c_A_CH_VitA_Tot1 c_A_CH_Deworm_Tot1
	make_index_gr health_utilization2 wgt stdgroup c_A_OutPat_Att_Total2 c_A_total_attendence2 c_A_OutPat_Ref_Total2 c_A_OutPat_Diag_Total2 c_A_TI_Total2  c_A_CI_Tot2 c_A_ANC_IPT2 c_A_ANC_Iron2 c_A_ANC_ITN2 c_A_ANC_Test_Syph2 c_A_Mat_Admit2 c_A_Mat_Deliver_InUnit2 c_A_Mat_Moth_VitA2 c_A_PN_Att2 c_A_CH_VitA_Tot2 c_A_CH_Deworm_Tot2 
	make_index_gr health_utilization3 wgt stdgroup c_A_OutPat_Att_Total3 c_A_total_attendence3 c_A_OutPat_Ref_Total3 c_A_OutPat_Diag_Total3 c_A_TI_Total3  c_A_CI_Tot3 c_A_ANC_IPT3 c_A_ANC_Iron3 c_A_ANC_ITN3 c_A_ANC_Test_Syph3 c_A_Mat_Admit3 c_A_Mat_Deliver_InUnit3 c_A_Mat_Moth_VitA3 c_A_PN_Att3 c_A_CH_VitA_Tot3 c_A_CH_Deworm_Tot3

forvalues i=1(1)3{	
	corr index_health_utilization`i' utilization_index`i'
	su index_health_utilization`i' utilization_index`i'
	su index_health_utilization`i' utilization_index`i' if treat==0
	su index_health_utilization`i' utilization_index`i' if treat==1
}
	
*********************************************************
// #10: correlate education indices and messages 
*********************************************************

**********************************************************************	
* relation between distance and messages at the cluster-level
**********************************************************************

**********************************************************************		
*Figure 18 in the Supplementary Information
**********************************************************************

ren RelevantMess_ct topicTotal
foreach	y in Education Health Water Total{
	scatter topic`y'  Arua_dist if treat==1 || lowess topic`y'  Arua_dist if treat==1,  lw(vthick) title("`y' messaging") ytitle("`y' messages (cluster)") xtitle("Distance to Arua (log)") legend(off)  name(topic`y')
	}

	graph combine topicTotal topicEducation topicHealth topicWater, title("Messaging and distance to district HQs")

**********************************************************************		
* Figure 23 in the Supplementary Information
**********************************************************************

graph drop _all
foreach	y in Education Health Water Total{
	scatter topic`y' c_poverty_census if treat==1 & c_poverty_census< 3|| lowess topic`y' c_poverty_census if treat==1 & c_poverty_census< 3,  lw(vthick) title("`y' messaging") ytitle("`y' messages (cluster)") xtitle("Wealth") legend(off)  name(topic`y')
	}

	graph combine topicTotal topicEducation topicHealth topicWater, title("Messaging and cluster wealth") 
			
**********************************************************************	
* change from baseline to midline against number of messages sent 
* 1. using Anderson weighted indices
* 2. using Kling unweighted indices 	
**********************************************************************	

lab var topicTotal "All relevant messages (cluster)"
lab var topicHealth "Health messages (cluster)"
	
foreach y in monitoring effort input utilization {
	gen D`y' = index_health_`y'2 - index_health_`y'1
	scatter D`y' topicTotal if treat==1 || lowess D`y' topicTotal if treat==1, lw(vthick) title("Health `y'") xtitle("All relevant messages") ytitle("Change `y'") name(`y'1) legend(off)	
	scatter D`y' topicHealth if treat==1 & topicHealth<20 || lowess D`y' topicHealth if treat==1 & topicHealth<20, lw(vthick) title("Health `y'") xtitle("Health messages") ytitle("Change `y'") name(`y'2) legend(off)	
	}

	graph combine monitoring1 effort1 input1 monitoring2 effort2 input2

**********************************************************************		
* Figure 15 in the Supplementary Information
**********************************************************************

foreach y in monitoring effort input utilization {
    gen D2`y' = `y'_index2-`y'_index1
	scatter D2`y' topicTotal if treat==1 || lowess D2`y' topicTotal if treat==1, lw(vthick) title("Health `y'") xtitle("All relevant messages") ytitle("Change `y'") name(`y'3) legend(off)	
	scatter D2`y' topicHealth if treat==1 & topicHealth<20 || lowess D2`y' topicHealth if treat==1 & topicHealth<20, lw(vthick) title("Health `y'") xtitle("Health messages") ytitle("Change `y'") name(`y'4) legend(off)	
	}

	graph combine monitoring3 effort3 input3 monitoring4 effort4 input4
	
**********************************************************************		
* baseline outcome (index) against number of messages sent 
* 1. using Anderson weighted indices
* 2. using Kling unweighted indices
**********************************************************************	

foreach y in monitoring effort input utilization{
	local l`y' : variable label index_health_`y'1
	scatter index_health_`y'1 topicHealth if treat==1 & topicHealth<20 || lowess index_health_`y'1 topicHealth if treat==1 & topicHealth<20,  lw(vthick) title("Health `y'") xtitle("N. health messages") ytitle(baseline `y') legend(off) name(`y'5)
}
	
	graph combine monitoring5 effort5 input5 utilization5

**********************************************************************		
* Figure 17 in Supplementary Information
**********************************************************************

* modertator: number of health messages
foreach y in monitoring effort input utilization{
	local l`y' : variable label `y'_index1
	scatter `y'_index1 topicHealth if treat==1 & topicHealth<20 || lowess `y'_index1 topicHealth if treat==1 & topicHealth<20,  lw(vthick) title("Health `y'") xtitle("N. health messages") ytitle(baseline `y') legend(off) name(`y'6)
}
	
	graph combine monitoring6 effort6 input6 utilization6
	
*********************************************************
// #11: save 
*********************************************************
note: ubridge_health_long.dta 
label data "updated health dataset (long): 06022017"
datasignature set , reset

saveold	ubridge_health_long.dta, replace
	
log close
clear
exit